package com.smart.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.smart.model.domain.ProjectTempTaskDO;
import com.smart.model.request.ProjectTempTaskRequest;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * @author zhanghong
 */
@Repository
public interface ProjectTempTaskMapper extends BaseMapper<ProjectTempTaskDO> {
    ProjectTempTaskDO selectDOById(Long id);
    List<ProjectTempTaskDO> selectDOList(ProjectTempTaskDO projectTempTask);
    int insertDO(ProjectTempTaskDO projectTempTask);
    int batchInsertDO(List<ProjectTempTaskDO> projectTempTaskDOList);
    int updateDO(ProjectTempTaskDO projectTempTask);
    int deleteDOById(Long id);
    int deleteDOByIds(String[] ids);

    @Select({"<script>",
            "   select count(1) from project_temp_task",
            "   <where>",
            "       deleted=0",
            "       <if test=\"param.nameOrTaskNum!=null and param.nameOrTaskNum!=''\">and (name like concat('%',#{param.nameOrTaskNum},'%') or task_num = #{param.nameOrTaskNum})</if>",
            "       <if test=\"param.startTime!=null and param.endTime!=null\">",
            "           and (",
            "               (start_time BETWEEN #{param.startTime} and #{param.endTime})",
            "               or",
            "               (end_time BETWEEN #{param.startTime} and #{param.endTime})",
            "               or",
            "               (start_time &lt; #{param.startTime} and end_time &gt; #{param.endTime})",
            "           )",
            "       </if>",
            "       <if test=\"param.status!=null and param.status!=''\">and status=#{param.status}</if>",
            "   </where>",
            "</script>"})
    long pageCount(@Param("param") ProjectTempTaskRequest param);
    @Select({"<script>",
            "   select * from project_temp_task",
            "   <where>",
            "       deleted=0",
            "       <if test=\"param.nameOrTaskNum!=null and param.nameOrTaskNum!=''\">and (name like concat('%',#{param.nameOrTaskNum},'%') or task_num = #{param.nameOrTaskNum})</if>",
            "       <if test=\"param.startTime!=null and param.endTime!=null\">",
            "           and (",
            "               (start_time BETWEEN #{param.startTime} and #{param.endTime})",
            "               or",
            "               (end_time BETWEEN #{param.startTime} and #{param.endTime})",
            "               or",
            "               (start_time &lt; #{param.startTime} and end_time &gt; #{param.endTime})",
            "           )",
            "       </if>",
            "       <if test=\"param.status!=null and param.status!=''\">and status=#{param.status}</if>",
            "   </where>",
            "   order by create_time desc limit #{limit} offset #{offset}",
            "</script>"})
    List<ProjectTempTaskDO> pageQuery(@Param("param") ProjectTempTaskRequest param, @Param("limit") int limit, @Param("offset") long offset);

    @Select("select * from project_temp_task where start_time <= now() and status=#{status}")
    List<ProjectTempTaskDO> listAlreadyStartByStatus(String status);

    @Select({"<script>",
            "   select count(1) from project_temp_task",
            "   <where>",
            "       deleted=0 ",
            "       and id in ",
            "       <foreach collection=\"taskTempIds\" item=\"item\" open=\"(\" separator=\",\" close=\")\"> ",
            "           #{item} ",
            "       </foreach> ",
            "       and status in",
            "       <foreach collection=\"statusList\" item=\"item\" open=\"(\" separator=\",\" close=\")\"> ",
            "           #{item} ",
            "       </foreach> ",
            "       <if test=\"nameOrTaskNum!=null and nameOrTaskNum!=''\">",
            "           and (name like concat('%',#{nameOrTaskNum},'%') or task_num like concat('%',#{nameOrTaskNum},'%'))",
            "       </if>",
            "       <if test=\"startTime!=null and endTime!=null\">",
            "           and (",
            "               (start_time BETWEEN #{startTime} and #{endTime})",
            "               or",
            "               (end_time BETWEEN #{startTime} and #{endTime})",
            "               or",
            "               (start_time &lt; #{startTime} and end_time &gt; #{endTime})",
            "           )",
            "       </if>",
            "   </where>",
            "</script>"})
    long count(@Param("taskTempIds") Set<Long> taskTempIds,
                                     @Param("statusList") List<String> statusList,
                                     @Param("nameOrTaskNum") String nameOrTaskNum,
                                     @Param("startTime") Date startTime,
                                     @Param("endTime") Date endTime);
    @Select({"<script>",
            "   select * from project_temp_task",
            "   <where>",
            "       deleted=0 ",
            "       and id in ",
            "       <foreach collection=\"taskTempIds\" item=\"item\" open=\"(\" separator=\",\" close=\")\"> ",
            "           #{item} ",
            "       </foreach> ",
            "       and status in",
            "       <foreach collection=\"statusList\" item=\"item\" open=\"(\" separator=\",\" close=\")\"> ",
            "           #{item} ",
            "       </foreach> ",
            "       <if test=\"nameOrTaskNum!=null and nameOrTaskNum!=''\">",
            "           and (name like concat('%',#{nameOrTaskNum},'%') or task_num like concat('%',#{nameOrTaskNum},'%'))",
            "       </if>",
            "       <if test=\"startTime!=null and endTime!=null\">",
            "           and (",
            "               (start_time BETWEEN #{startTime} and #{endTime})",
            "               or",
            "               (end_time BETWEEN #{startTime} and #{endTime})",
            "               or",
            "               (start_time &lt; #{startTime} and end_time &gt; #{endTime})",
            "           )",
            "       </if>",
            "   </where>",
            "   order by create_time desc limit #{limit} offset #{offset}",
            "</script>"})
    List<ProjectTempTaskDO> listPage(@Param("taskTempIds") Set<Long> taskTempIds,
                                     @Param("statusList") List<String> statusList,
                                     @Param("nameOrTaskNum") String nameOrTaskNum,
                                     @Param("startTime") Date startTime,
                                     @Param("endTime") Date endTime,
                                     @Param("limit") int limit,
                                     @Param("offset") long offset);

    @Select("select * from project_temp_task order by task_code_date desc, task_code_num desc limit 1")
    ProjectTempTaskDO getRecentOne();

    /**
     * 将已过期，任务状态为未开始的更新为已结束
     * @return
     */
    Integer updateExpiredUnStartTask();

    Integer updateStatus(@Param("id") Long id, @Param("status") String status);

    List<ProjectTempTaskDO> listByStartTime(@Param("startTime") Date startTime);

    List<ProjectTempTaskDO> listByEndTime(@Param("endTime") Date endTime);

}
